
package com.qiyigu.itc.speech.xunfai;

/**
 * 讯飞唤醒
 */

public class XunfaiWarkUpServiceImpl {//extends BaseServiceImpl implements WarkUpService {
/*
    protected static XunfaiWarkUpServiceImpl service = null;
    // 语音唤醒对象
    private VoiceWakeuper mIvw;
    private int curThresh = 10;//设置门限值 -20至60

    protected OnSdkWarkUpListener onSdkWarkUpListener;

    protected XunfaiWarkUpServiceImpl(OnSdkWarkUpListener onSdkWarkUpListener) {
        service = this;
        this.onSdkWarkUpListener = onSdkWarkUpListener;
        SpeechUtility.createUtility(ItcBaseApp.getInstance(), "appid=" + Constant
                .XUN_FAI_APPKEY);
        // 初始化唤醒对象
        mIvw = VoiceWakeuper.createWakeuper(ItcBaseApp.getInstance(), null);
    }

    public static synchronized XunfaiWarkUpServiceImpl getInstance(OnSdkWarkUpListener
                                                                           onSdkWarkUpListener) {
        if (service == null) {
            service = new XunfaiWarkUpServiceImpl(onSdkWarkUpListener);
        }
        return service;
    }

    @Override
    public synchronized void startWarkUp() {
        mIvw = VoiceWakeuper.getWakeuper();
        if (mIvw != null) {
            // 清空参数
            mIvw.setParameter(SpeechConstant.PARAMS, null);
            // 唤醒门限值，根据资源携带的唤醒词个数按照“id:门限;id:门限”的格式传入
            mIvw.setParameter(SpeechConstant.IVW_THRESHOLD, "0:" + curThresh);
            // 设置唤醒模式
            mIvw.setParameter(SpeechConstant.IVW_SST, "wakeup");
            // 设置持续进行唤醒
            mIvw.setParameter(SpeechConstant.KEEP_ALIVE, "1");
            // 设置闭环优化网络模式
            mIvw.setParameter(SpeechConstant.IVW_NET_MODE, "0");
            // 设置唤醒资源路径
            mIvw.setParameter(SpeechConstant.IVW_RES_PATH, getResource());
            // 设置唤醒录音保存路径，保存最近一分钟的音频
            mIvw.setParameter(SpeechConstant.IVW_AUDIO_PATH, PathUtils.getTmpDirPath() +
                    "msc/ivw.wav");
            mIvw.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
            // 如有需要，设置 NOTIFY_RECORD_DATA 以实时通过 onEvent 返回录音音频流字节
            //mIvw.setParameter( SpeechConstant.NOTIFY_RECORD_DATA, "1" );

            // 启动唤醒
            mIvw.startListening(mWakeuperListener);
            Log.d(TAG, "startWarkUp Xunfai startListening: ");
        } else {
            mIvw = VoiceWakeuper.createWakeuper(ItcBaseApp.getInstance(), null);
            logger.e("讯飞唤醒未初始化,重新createWakeuper");
        }
    }

    private String getResource() {
        final String resPath = ResourceUtil.generateResourcePath(ItcBaseApp.getInstance(),
                ResourceUtil.RESOURCE_TYPE
                        .assets, "xunfai/ivw/" + Constant.XUN_FAI_APPKEY + ".jet");
        Log.d(TAG, "resPath: " + resPath);
        return resPath;
    }

    @Override
    public synchronized void stopWarkUp() {
        if (null != this.mIvw) {
            mIvw.stopListening();
        }
    }

    @Override
    public synchronized void releaseWarkUp() {
        if (mIvw != null) {
            mIvw.destroy();
        }
    }

    private WakeuperListener mWakeuperListener = new WakeuperListener() {

        @Override
        public void onResult(WakeuperResult result) {
            if (result == null) {
                return;
            }
            try {
                String text = result.getResultString();
                Log.d(TAG, " WakeuperListener onResult" + text);
                JSONObject object;
                object = new JSONObject(text);
                String strResult = object.optString("sst");
                if (StringUtils.isNotEmpty(strResult) && strResult.contains("warkup")) {
                    if (onSdkWarkUpListener != null) {
                        onSdkWarkUpListener.onWarkUpReady();
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

        @Override
        public void onError(SpeechError error) {

        }

        @Override
        public void onBeginOfSpeech() {
        }

        @Override
        public void onEvent(int eventType, int isLast, int arg2, Bundle obj) {
            switch (eventType) {
                // EVENT_RECORD_DATA 事件仅在 NOTIFY_RECORD_DATA 参数值为 真 时返回
                case SpeechEvent.EVENT_RECORD_DATA:
                    final byte[] audio = obj.getByteArray(SpeechEvent.KEY_EVENT_RECORD_DATA);
                    Log.d(TAG, "ivw audio length: " + audio.length);
                    break;
            }
        }

        @Override
        public void onVolumeChanged(int volume) {

        }
    };*/
}

